<template>
  <el-dialog
    :title="title"
    :visible.sync="modify"
    v-if="modify"
    width="60%"
    append-to-body
    :before-close="handleClose"
    @close="CloseIn"
  >
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="基本信息" name="userForm">
        <el-form ref="formRef" :model="form" label-width="100px">
          <el-row>
            <el-col :span="8">
              <el-form-item
                v-if="shouldDisplay('avatar')"
                :rules="getFieldRules('avatar')"
                label="照片"
                prop="avatar"
              >
                <uploadImg
                  @imageUrl="imageUrl"
                  :avatar="form.avatar"
                  :disabled="!canModify('avatar')"
                  style="border: 1px solid #d9d9d9"
                ></uploadImg>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                v-if="shouldDisplay('name')"
                label="姓名"
                prop="name"
              >
                <el-input
                  placeholder="请输入姓名"
                  v-model.trim="form.name"
                  :disabled="!canModify('name')"
                  maxlength="10"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                v-if="shouldDisplay('sex')"
                :rules="getFieldRules('sex')"
                label="性别"
                prop="sex"
              >
                <el-select
                  v-model="form.sex"
                  placeholder="请选择"
                  :disabled="!canModify('sex')"
                  filterable
                  clearable
                  class="elSelect"
                >
                  <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item
                v-if="shouldDisplay('registerCode')"
                :rules="getFieldRules('registerCode')"
                label="注册号"
                prop="registerCode"
              >
                <el-input
                  v-model.trim="form.registerCode"
                  placeholder="请输入注册号"
                  :disabled="!canModify('registerCode')"
                  maxlength="15"
                />
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item
                v-if="shouldDisplay('sno')"
                :rules="getFieldRules('sno')"
                label="学号"
                prop="sno"
              >
                <el-input
                  v-model.trim="form.sno"
                  placeholder="请输入学号"
                  :disabled="!canModify('sno')"
                  maxlength="15"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="sfzjlxdm"
                v-if="shouldDisplay('sfzjlxdm')"
                :rules="getFieldRules('sfzjlxdm')"
                label="证件类型"
              >
                <el-select
                  v-model="form.sfzjlxdm"
                  clearable
                  filterable
                  placeholder="请选择证件类型"
                  :disabled="!canModify('sfzjlxdm')"
                >
                  <el-option
                    v-for="dict in dict.type.bc_resident_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                v-if="shouldDisplay('cardCode')"
                
                label="身份证号"
                prop="cardCode"
              >
                <el-input
                  v-model.trim="form.cardCode"
                  placeholder="请输入身份证号"
                  @blur="codeBulr"
                  :disabled="!canModify('cardCode')"
                  maxlength="18"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                v-if="shouldDisplay('birthday')"
                :rules="getFieldRules('birthday')"
                label="出生日期"
                prop="birthday"
              >
                <!-- <el-input v-model.trim="form.birthday" placeholder="出生日期" disabled/> -->

                <el-date-picker
                  :disabled="!canModify('birthday')"
                  class="elSelect"
                  v-model="form.birthday"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                v-if="shouldDisplay('nation')"
                :rules="getFieldRules('nation')"
                label="民族"
                prop="nation"
              >
                <el-select
                  v-model="form.nation"
                  placeholder="请选择"
                  :disabled="!canModify('nation')"
                  clearable
                  filterable
                  class="elSelect"
                >
                  <el-option
                    v-for="dict in dict.type.bc_nation_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="nativePlace"
                v-if="shouldDisplay('nativePlace')"
                :rules="getFieldRules('nativePlace')"
                label="籍贯"
              >
                <!-- <el-input v-model.trim="form.nativePlace" placeholder="请输入籍贯" maxlength="20" /> -->
                <el-cascader
                  v-model="form.nativePlace"
                   class="elSelect"
                  :disabled="!canModify('nativePlace')"
                  :props="{
                    value: 'unitCode',
                    currentLabels: 'label',
                    checkStrictly: true,
                  }"
                  :options="options"
                  @change="handlePlace"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="political"
                v-if="shouldDisplay('political')"
                :rules="getFieldRules('political')"
                label="政治面貌"
              >
                <el-select
                  v-model="form.political"
                  placeholder="请选择"
                  :disabled="!canModify('political')"
                  clearable
                  filterable
                  class="elSelect"
                >
                  <el-option
                    v-for="dict in dict.type.bc_political_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="residence"
                v-if="shouldDisplay('residence')"
                :rules="getFieldRules('residence')"
                label="户籍所在地"
              >
                <el-input
                  v-model.trim="form.residence"
                  :disabled="!canModify('residence')"
                  placeholder="请输入户籍所在地"
                  maxlength="50"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="deptId"
                v-if="shouldDisplay('deptId')"
                :rules="getFieldRules('deptId')"
                label="所属院系"
              >
                <el-select
                  v-model="form.deptId"
                  placeholder="请选择"
                  clearable
                  filterable
                  class="elSelect"
                  @change="handleChange"
                  :disabled="!canModify('deptId')"
                >
                  <el-option
                    v-for="item in deptOptions"
                    :key="item.deptId"
                    :label="item.deptName"
                    :value="item.deptId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="specialityId"
                v-if="shouldDisplay('specialityId')"
                :rules="getFieldRules('specialityId')"
                label="专业"
              >
                <el-select
                  v-model="form.specialityId"
                  placeholder="请选择"
                  clearable
                  filterable
                  class="elSelect"
                  @change="changeGrade"
                  :disabled="!canModify('specialityId')"
                >
                  <el-option
                    v-for="item in profession"
                    :key="item.VALUE"
                    :label="item.TITILE"
                    :value="item.VALUE"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="gradeId"
                v-if="shouldDisplay('gradeId')"
                :rules="getFieldRules('gradeId')"
                label="年级"
              >
                <el-select
                  v-model="form.gradeId"
                  placeholder="请选择"
                  @change="changeGrade"
                  clearable
                  filterable
                  :disabled="!canModify('gradeId')"
                  class="elSelect"
                >
                  <el-option
                    v-for="item in gradeDate"
                    :key="item.VALUE"
                    :label="item.TITILE"
                    :value="item.VALUE"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="classId"
                v-if="shouldDisplay('classId')"
                :rules="getFieldRules('classId')"
                label="班级"
              >
                <el-select
                  v-model="form.classId"
                  placeholder="请选择"
                  :disabled="!canModify('classId')"
                  clearable
                  filterable
                  class="elSelect"
                >
                  <el-option
                    v-for="item in classList"
                    :key="item.VALUE"
                    :label="item.TITLE"
                    :value="item.VALUE"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="studentType"
                v-if="shouldDisplay('studentType')"
                :rules="getFieldRules('studentType')"
                label="学生类型"
              >
                <el-select
                  v-model="form.studentType"
                  placeholder="请选择"
                  :disabled="!canModify('studentType')"
                  clearable
                  filterable
                  class="elSelect"
                >
                  <el-option
                    v-for="dict in dict.type.bc_student_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="joinDate"
                v-if="shouldDisplay('joinDate')"
                :rules="getFieldRules('joinDate')"
                label="入学时间"
              >
                <el-date-picker
                  class="elSelect"
                  clearable
                  v-model="form.joinDate"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  :disabled="!canModify('joinDate')"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="guaduateDate"
                v-if="shouldDisplay('guaduateDate')"
                :rules="getFieldRules('guaduateDate')"
                label="毕业时间"
              >
                <el-date-picker
                  class="elSelect"
                  clearable
                  v-model="form.guaduateDate"
                  type="date"
                  placeholder="选择日期"
                  :disabled="!canModify('guaduateDate')"
                  value-format="yyyy-MM-dd"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="nodeType"
                v-if="shouldDisplay('nodeType')"
                :rules="getFieldRules('nodeType')"
                label="户口类别"
              >
                <el-select
                  v-model="form.nodeType"
                  placeholder="请选择"
                  :disabled="!canModify('nodeType')"
                  clearable
                  filterable
                  class="elSelect"
                >
                  <el-option
                    v-for="dict in dict.type.bc_node_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="mobile"
                v-if="shouldDisplay('mobile')"
                :rules="getFieldRules('mobile')"
                label="联系电话"
              >
                <el-input
                  v-model.trim="form.mobile"
                  :disabled="!canModify('mobile')"
                  placeholder="请输入联系电话"
                  maxlength="11"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="email"
                v-if="shouldDisplay('email')"
                :rules="getFieldRules('email')"
                label="邮箱"
              >
                <el-input
                  v-model.trim="form.email"
                  placeholder="请输入邮箱"
                  :disabled="!canModify('email')"
                  maxlength="30"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="qq"
                v-if="shouldDisplay('qq')"
                :rules="getFieldRules('qq')"
                label="qq"
              >
                <el-input
                  v-model.trim="form.qq"
                  placeholder="请输入qq号"
                  :disabled="!canModify('qq')"
                  maxlength="12"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="wechatCode"
                v-if="shouldDisplay('wechatCode')"
                :rules="getFieldRules('wechatCode')"
                label="微信"
              >
                <el-input
                  v-model.trim="form.wechatCode"
                  placeholder="请输入微信号"
                  :disabled="!canModify('wechatCode')"
                  maxlength="30"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="healthStatus"
                v-if="shouldDisplay('healthStatus')"
                :rules="getFieldRules('healthStatus')"
                label="健康状态"
              >
                <el-select
                  v-model="form.healthStatus"
                  placeholder="请选择"
                  clearable
                  filterable
                  :disabled="!canModify('healthStatus')"
                  class="elSelect"
                >
                  <el-option
                    v-for="dict in dict.type.bc_health_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="emergencyMobile"
                v-if="shouldDisplay('emergencyMobile')"
                :rules="getFieldRules('emergencyMobile')"
                label="家长电话"
              >
                <el-input
                  v-model.trim="form.emergencyMobile"
                  placeholder="请输入家长电话"
                  maxlength="11"
                  :disabled="!canModify('emergencyMobile')"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="studentStatus"
                v-if="shouldDisplay('studentStatus')"
                :rules="getFieldRules('studentStatus')"
                label="学籍状态"
              >
                <el-select
                  v-model="form.studentStatus"
                  placeholder="请选择"
                  :disabled="!canModify('studentStatus')"
                  learable
                  filterable
                  class="elSelect"
                >
                  <el-option
                    v-for="dict in dict.type.bc_student_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="ofStudent"
                v-if="shouldDisplay('ofStudent')"
                :rules="getFieldRules('ofStudent')"
                label="生源地"
              >
                <el-input
                  v-model.trim="form.ofStudent"
                  placeholder="请输入生源地"
                  maxlength="30"
                  :disabled="!canModify('ofStudent')"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="atSchool"
                v-if="shouldDisplay('atSchool')"
                :rules="getFieldRules('atSchool')"
                label="在校状态"
              >
                <el-select
                  v-model="form.atSchool"
                  clearable
                  filterable
                   class="elSelect"
                  placeholder="请选择在校状态"
                  :disabled="!canModify('atSchool')"
                >
                  <el-option
                    v-for="dict in dict.type.basic_at_school"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="highSchool"
                v-if="shouldDisplay('highSchool')"
                :rules="getFieldRules('highSchool')"
                label="毕业高中"
              >
                <el-input
                  v-model.trim="form.highSchool"
                  placeholder="请输入毕业高中"
                  maxlength="30"
                  :disabled="!canModify('highSchool')"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="address"
                v-if="shouldDisplay('address')"
                :rules="getFieldRules('address')"
                label="家庭地址"
              >
                <el-cascader
                  v-model="form.address"
                  class="elSelect"
                  :disabled="!canModify('address')"
                  :props="{
                    value: 'unitCode',
                    currentLabels: 'label',
                    checkStrictly: true,
                  }"
                  filterable
                  :options="addRessOptions"
                  @change="handleAddressPlace"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                prop="addressInfo"
                v-if="shouldDisplay('addressInfo')"
                :rules="getFieldRules('addressInfo')"
                label="家庭详细地址"
                class="elSelect"
              >
                <el-input
                  v-model.trim="form.addressInfo"
                  placeholder="请输入家庭详细地址"
                  maxlength="30"
                  :disabled="!canModify('addressInfo')"
                />
              </el-form-item>
            </el-col>
            
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="家庭信息" name="familyForm">
        <el-button type="primary" @click="addItem">新增行</el-button>
        <el-form :model="familyFormObj" label-width="90px" ref="familyForms">
          <el-row>
            <el-form-item
              v-for="(item, index) in familyFormObj.studentFamilyList"
              :key="index"
              style="margin-left: -90px"
            >
              <el-col :span="8">
                <el-form-item
                  v-if="shouldDisplay('reasonStatus')"
                  :rules="getFieldRules('reasonStatus')"
                  label="与本人关系"
                  :prop="'studentFamilyList.' + index + '.reasonStatus'"
                >
                  <el-select
                    v-model="item.reasonStatus"
                    placeholder="请选择"
                    filterable
                    class="elSelect"
                    :disabled="!canModify('reasonStatus')"
                  >
                    <el-option
                      v-for="dict in dict.type.bc_student_famliy"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  v-if="shouldDisplay('name')"
                  :rules="getFieldRules('name')"
                  label="姓名"
                  :prop="'studentFamilyList.' + index + '.name'"
                >
                  <el-input
                    placeholder="请输入姓名"
                    v-model.trim="item.name"
                    :disabled="!canModify('name')"
                    maxlength="10"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  v-if="shouldDisplay('familyMobile')"
                  :rules="getFieldRules('familyMobile')"
                  label="联系方式"
                  :prop="'studentFamilyList.' + index + '.familyMobile'"
                >
                  <el-input
                    placeholder="请输入联系方式"
                    v-model.trim="item.familyMobile"
                    :disabled="!canModify('familyMobile')"
                    maxlength="11"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  v-if="shouldDisplay('wechatCode')"
                  :rules="getFieldRules('wechatCode')"
                  label="微信号"
                  :prop="'studentFamilyList.' + index + '.wechatCode'"
                >
                  <el-input
                    placeholder="请输入微信号"
                    v-model.trim="item.wechatCode"
                    :disabled="!canModify('wechatCode')"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  v-if="shouldDisplay('unit')"
                  :rules="getFieldRules('unit')"
                  label="工作单位"
                  :prop="'studentFamilyList.' + index + '.unit'"
                >
                  <el-input
                    placeholder="请输入工作单位"
                    v-model.trim="item.unit"
                    :disabled="!canModify('unit')"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  v-if="shouldDisplay('familyAddress')"
                  :rules="getFieldRules('familyAddress')"
                  label="家庭地址"
                  :prop="'studentFamilyList.' + index + '.familyAddress'"
                >
                  <el-cascader
                    v-model="item.familyAddress"
                    class="elSelect"
                    :disabled="!canModify('familyAddress')"
                    :props="{
                      value: 'unitCode',
                      currentLabels: 'label',
                      checkStrictly: true,
                    }"
                    :options="addRessOptions"
                    @change="handlefamilyAddPlace"
                  ></el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  v-if="shouldDisplay('postalCode')"
                  :rules="getFieldRules('postalCode')"
                  label="家庭邮编"
                  :prop="'studentFamilyList.' + index + '.postalCode'"
                >
                  <el-input
                    placeholder="请输入家庭邮编"
                    v-model.trim="item.postalCode"
                    :disabled="!canModify('postalCode')"
                    maxlength="11"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  v-if="shouldDisplay('familyRemark')"
                  :rules="getFieldRules('familyRemark')"
                  label="家庭备注"
                  :prop="'studentFamilyList.' + index + '.familyRemark'"
                >
                  <el-input
                    placeholder="贫困户或建档立卡户 有就填写没有就写无"
                    v-model.trim="item.familyRemark"
                    :disabled="!canModify('familyRemark')"
                    maxlength="11"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="1"
                ><el-button
                  @click.prevent="removeDomain(index)"
                  style="margin-left: 5px"
                  >删除行</el-button
                ></el-col
              >
            </el-form-item>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="学生获奖信息" name="awadForm">
        <el-button type="primary" @click="addawadFormItem">新增行</el-button>
        <el-form :model="awadFormObj" ref="awadForm" label-width="80px">
          <el-row>
            <el-form-item
              v-for="(item, index) in awadFormObj.studentAwardsList"
              :key="index"
              style="margin-left: -80px"
            >
              <el-col :span="6">
                <el-form-item
                  v-if="shouldDisplay('awardName')"
                  :rules="getFieldRules('awardName')"
                  label="奖项名称"
                  :prop="'studentAwardsList.' + index + '.awardName'"
                >
                  <el-input
                    placeholder="请输入奖项名称"
                    v-model.trim="item.awardName"
                    :disabled="!canModify('awardName')"
                    maxlength="20"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  v-if="shouldDisplay('awardLevel')"
                  :rules="getFieldRules('awardLevel')"
                  label="奖项级别"
                  :prop="'studentAwardsList.' + index + '.awardLevel'"
                >
                  <el-select
                    v-model="item.awardLevel"
                    placeholder="请选择"
                    :disabled="!canModify('awardLevel')"
                    learable
                    filterable
                    class="elSelect"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="dict in dict.type.basic_awads_level"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  v-if="shouldDisplay('issuingAuthority')"
                  :rules="getFieldRules('issuingAuthority')"
                  label="颁发机构"
                  :prop="'studentAwardsList.' + index + '.issuingAuthority'"
                >
                  <el-input
                    placeholder="请输入颁发机构"
                    v-model.trim="item.issuingAuthority"
                    :disabled="!canModify('issuingAuthority')"
                    maxlength="30"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  v-if="shouldDisplay('awardDate')"
                  :rules="getFieldRules('awardDate')"
                  label="获奖日期"
                  :prop="'studentAwardsList.' + index + '.awardDate'"
                >
                  <el-date-picker
                    class="elSelect"
                    clearable
                    v-model="item.awardDate"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="1"
                ><el-button
                  @click.prevent="removeAward(index)"
                  style="margin-left: 5px"
                  >删除行</el-button
                ></el-col
              >
            </el-form-item>
          </el-row>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveInfo">确 定</el-button>
      <el-button @click="closeDia">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Validators from "@/utils/validate.js";
import uploadImg from "@/components/ImageUpload/imageIndexs";
import { getClassSelect, getProvinceTree } from "@/api/information/classInfor";
import { getSpecialityByDeptId } from "@/api/information/unitMajor";
import { getGradeSelect } from "@/api/information/gradeInfor";
import { getFormInfo, addersTree } from "@/api/flowable/stuapply.js";
import {getInfo} from '@/api/login.js'
export default {
  components: { uploadImg },
  props: {
    title: { type: String },
    deptOptions: {
      type: Array,
      default: function () {
        return [];
      },
    },
    // addRessOptions: {
    //   type: Array,
    //   default: function () {
    //     return [];
    //   },
    // },
    showType: {
      type: String,
      default: "operate",
    },
  },
  dicts: [
    "bc_student_status", // 学籍状态
    "bc_political_type", // 政治面貌
    "bc_nation_type", // 民族
    "bc_student_type", // 学生类型
    "bc_node_type", // 入学前户口类别
    "follow_status", // 是否关注对象
    "sys_user_sex", // 性别
    "bc_student_famliy", // 家庭关系
    "bc_health_status", // 健康状态
    "basic_awads_level", // 获奖级别
    "basic_at_school", // 是否在校
    "bc_resident_type", // 证件类型
  ],
  computed: {
    // userstudent() {
    //   return this.$store.state.user.userInfor.student;
    // },
  },
  data() {
    return {
      modify: false,
      activeName: "userForm",
      classList: [],
      profession: [],
      gradeDate: [],
      options: [],
      fieldConfig: [],
      itemRules: [],
      form: this.forms,
      familyFormObj: {
        studentFamilyList: [
          {
            reasonStatus: "",
            name: "",
            familyMobile: "",
            familyAddress: "",
            postalCode: "",
          },
        ],
      },
       fieldRegexConfig: {
      mobile: "^1[3-9]\\d{9}$",
      email: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
      emergencyMobile: "^1[3-9]\\d{9}$",
      familyMobile: "^1[3-9]\\d{9}$",
      name: "^[\u4e00-\u9fa5]+$",
      cardCode: "^[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[0-9Xx]$"
    },
      awadFormObj: {
        studentAwardsList: [
          {
            awardName: "",
            awardLevel: "",
            issuingAuthority: "",
            awardDate: "",
          },
        ],
      },
      initialFormData:{}
    };
  },

  watch: {
    
  },
  created() {
 
  //  updata
    this.updata()
    // console.log(this.fieldConfig, this.forms, "this.fieldConfig1111");
    this.getTree();

    addersTree().then((res) => {
      console.log(res, "家庭地址");
      this.addRessOptions = res.data;
    });
    
  },
  methods: {
    updata() {
      // console.log(this.fieldConfig, this.forms, "this.fieldConfig22222222");
      // created只会执行一次，但是又想监听改变的值做其他事情，就要使用methods 来代替 created
      getInfo().then(res => {
        console.log(res,'ddd')
        console.log(res,res.user.student.studentFamilyList,'详情')
         this.form = res.user.student
          this.initialFormData = { ...this.form };
         if(res.user.student.studentFamilyList.length> 0){
         this.familyFormObj.studentFamilyList = res.user.student.studentFamilyList || this.familyFormObj.studentFamilyList
        console.log(res,res.user.student.studentFamilyList,'12313')
         }else if(res.user.student.studentAwardsList.length>0){
         this.awadFormObj.studentAwardsList = res.user.student.studentAwardsList || this.awadFormObj.studentAwardsList
         }else if(this.form) {
this.getGradeInfor();
      this.getSpeciality();
      this.changeGrade();
         }
      
      })

    },
    handleClick() {
      getFormInfo(this.activeName).then((res) => {
        console.log(res,'fdsfd')
        this.fieldConfig = res.data.map((item) => {
          if (item.filedName == "mobile") {
            return {
              ...item,
              regex: "^1[3-9]\\d{9}$", // 修正：使用 \\d 而不是 \d
            };
          } else if (item.fieldName == "email") {
            return {
              ...item,
              regex: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", // 修正：转义点号
            };
          } else if (item.fieldName == "emergencyMobile") {
            return {
              ...item,
              regex: "^1[3-9]\\d{9}$", // 修正：使用 \\d 而不是 d
            };
          } else if (item.fieldName == "familyMobile") {
            return {
              ...item,
              regex: "^1[3-9]\\d{9}$", // 修正：使用 \\d 而不是 d
            };
          } else if (item.fieldName == "name") {
            return {
              ...item,
              regex: "^[\u4e00-\u9fa5]+$",
            };
          } else {
            return {
              ...item,
              regex: "",
            };
          }
        });
        console.log(this.fieldConfig,this.form, "this.fieldConfig ");
      }).catch(err => {
        console.log(err,'err')
      })
    },
    imageUrl(data) {
      console.log(data,'图片')
      this.form.avatar = data;
    },
    // 新增行
    addItem() {
      this.familyFormObj.studentFamilyList.push({
        reasonStatus: "",
        name: "",
        familyMobile: "",
        familyAddress: "",
        postalCode: "",
        wechatCode: "",
        unit: "",
        familyRemark: "",
      }); // 添加新行
    },
    removeDomain(index) {
      if (this.familyFormObj.studentFamilyList.length > 1) {
        this.familyFormObj.studentFamilyList.splice(index, 1);
      } else {
      }
    },
    // 删除行
     removeAward(index) {
      if (this.awadFormObj.studentAwardsList.length > 1) {
        this.awadFormObj.studentAwardsList.splice(index, 1);
      } else {
        console.log(index)
         this.awadFormObj.studentAwardsList.splice(index, 1);
      }
    },
     handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
    // 新增行
    addawadFormItem() {
      this.awadFormObj.studentAwardsList.push({
        awardName: "",
        awardLevel: "",
        issuingAuthority: "",
        awardDate: "",
      }); // 添加新行
    },
    openEdit(data) {
      console.log(data,this.form,'tadamd')
      this.modify = true;
      //  this.updata()
      let formData = JSON.parse(data.parameter);
      this.awadFormObj.studentAwardsList = formData.studentAwardsList;
      this.familyFormObj.studentFamilyList = formData.studentFamilyList;
      this.form = { ...this.form, ...formData, baiId:data.baiId,apprStatus: data.apprStatus,studentId:data.id};
      // this.form = data;
      console.log(
        this.form,data.baiId,
        "thisifoa甲方收到甲方收到甲方"
      );
     
      this.handleClick();
    },
    openDia() {
     
      this.form = {};
      (this.familyFormObj.studentFamilyList = [
        {
          reasonStatus: "",
          name: "",
          familyMobile: "",
          familyAddress: "",
          postalCode: "",
          wechatCode: "",
          unit: "",
          familyRemark: "",
        },
      ]),
        // 添加新行
        (this.awadFormObj.studentAwardsList = [
          {
            awardName: "",
            awardLevel: "",
            issuingAuthority: "",
            awardDate: "",
          },
        ]);
        this.updata()
      this.handleClick();
    },
    closeDia() {
      this.modify = false;
      this.$refs.formRef.resetFields();
      this.$refs.awadForm.resetFields();
      this.$refs.familyForm.resetFields();
    },
    // 判断字段是否应该显示
    shouldDisplay(fieldName) {
      // console.log(this.fieldConfig, this.form, "this.fieldConfig");
      const field = this.fieldConfig.find(
        (item) => item.filedName === fieldName
      );
      return field ? field.isDisplay === "Y" : false;
    },

    // 判断字段是否可以修改
    canModify(fieldName) {
      const field = this.fieldConfig.find(
        (item) => item.filedName === fieldName
      );
      return field ? field.isModify === "Y" : true;
    },

   getFieldRules(fieldName) {
  const field = this.fieldConfig.find(item => item.filedName === fieldName);
  const rules = [];

  if (field && field.isRequired === "Y") {
    rules.push({
      required: true,
      message: `${field.nickName}不能为空`,
      trigger: "blur"
    });
  }

  // 添加正则验证
  if (this.fieldRegexConfig[fieldName]) {
    rules.push({
      pattern: new RegExp(this.fieldRegexConfig[fieldName]),
      message: `${field?.nickName || fieldName}格式不正确`,
      trigger: "blur"
    });
  }

  // 特殊字段的额外验证
  if (fieldName === 'cardCode') {
    rules.push({
      validator: this.validateIdCard,
      trigger: "blur"
    });
  }

  return rules;
},

// 添加身份证验证方法
validateIdCard(rule, value, callback) {
  if (!value) {
    callback();
    return;
  }
  
  // 身份证简单验证
  const reg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$/;
  if (!reg.test(value)) {
    callback(new Error('身份证格式不正确'));
  } else {
    // 校验码验证
    const idCard = value.toUpperCase();
    if (idCard.length === 18) {
      const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
      const parity = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
      let sum = 0;
      
      for (let i = 0; i < 17; i++) {
        sum += parseInt(idCard.charAt(i)) * factor[i];
      }
      
      if (parity[sum % 11] !== idCard.charAt(17)) {
        callback(new Error('身份证校验码不正确'));
        return;
      }
    }
    callback();
  }
},

// 移除 handleClick 方法中的正则设置部分
handleClick() {
  getFormInfo(this.activeName).then((res) => {
    console.log(res.code,'fsddffffffffffffffffffffffffffffff')
    this.fieldConfig = res.data;
     this.modify = true;
    console.log(this.fieldConfig, "this.fieldConfig ");
  }).catch(error =>{
      console.log('500错误代码:', error);
    if (error =='采集信息还未开始') {
    this.modify = false;
    }
  })
},
    submitForm() {
      // 输出修改过的字段
      // 这里可以调用API，传递 modifiedFields 对象
    },
    getModifiedFields() {
      const originalForm = this.form; // 假设这是原始表单数据，未修改的状态
      return Object.keys(this.form).reduce((acc, key) => {
        if (this.form[key] !== originalForm[key]) {
          acc[key] = this.form[key];
        }
        return acc;
      }, {});
    },

    saveInfo() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$refs.familyForms.validate((valid) => {
            if (valid) {
              console.log("cheng",this.form);
              this.$refs.awadForm.validate((valid) => {
                if (valid) {
                  const modifiedData = {};
                  for (const key in this.form) {
                    if (this.form[key] !== this.initialFormData[key]) {
                      modifiedData[key] = this.form[key];
                    }
                  }
                  this.familyFormObj.studentFamilyList =
                    this.familyFormObj.studentFamilyList.map((item) => {
                      const newItem = { ...item };
                      if (
                        Array.isArray(newItem.familyAddress) &&
                        newItem.familyAddress.length > 0
                      ) {
                        newItem.familyAddress = String(
                          newItem.familyAddress[
                            newItem.familyAddress.length - 1
                          ]
                        ); 
                      }
                      return newItem;
                    });
                  // 模拟提交修改后的数据
                  this.form = {
                    ...modifiedData,
                    ...this.familyFormObj,
                    ...this.awadFormObj,
                    id: this.form.id,
                  };
                  console.log(
                    this.form,
                    this.familyFormObj,
                    this.awadFormObj,
                    "提交的值"
                  );
                  console.log("提交的修改数据:",this.form, modifiedData);

                  if(this.form.baiId) {
                  console.log("有baiId:",this.form, modifiedData);

                    this.$emit("savePut", this.form);

                  }else{
                  console.log("无baiID:",this.form, modifiedData);

                    this.$emit("saveSuccess", JSON.stringify(this.form));
                  }
                  // 可以在这里处理表单提交逻辑
                } else {
                  this.$modal.msgError("请填写获奖信息");
                }
              });
              // 可以在这里处理表单提交逻辑
            } else {
              this.$modal.msgError("请输入家庭信息");
            }
          });
          // 可以在这里处理表单提交逻辑
        } else {
          this.$modal.msgError("请填写信息");
        }
      });
      // 告诉父组件，保存成功
    },
    // 清除表单
    CloseIn() {
      this.form = {
        specialityId: undefined,
        classId: undefined,
      };
      this.modify = false;
    },
    // 根据身份证号获取出生年月日
    getBirthday(idCard) {
      // 提取生日信息
      var birthday = idCard.substr(6, 8);
      // 解析年、月、日
      var year = birthday.substr(0, 4);
      var month = birthday.substr(4, 2);
      var day = birthday.substr(6, 2);

      // 返回生日信息
      return year + "-" + month + "-" + day;
    },
    // 籍贯选择赋值
    handlePlace(data) {
      this.form.nativePlace = data[data.length - 1];
    },
    // 籍贯选择赋值
    handleAddressPlace(data) {
      this.form.address = data[data.length - 1];
    },
    // 籍贯选择赋值
    handlefamilyAddPlace(data) {
      this.form.familyAddress = data[data.length - 1];
      console.log(this.form.familyAddress, "this.form.familyAddress");
    },
    // 下拉树选择
    handleChange(value) {
      this.form.specialityId = "";
      // if (typeof value == "object") {
      //   this.form.deptId = value[value.length - 1];
      //   // this.queryParams.deptId = value[value.length - 1];
      //   this.getSpeciality();
      // } else if (typeof value == "number") {
      //   this.form.deptId = value;
      //   this.getSpeciality();
      // } else if (value == undefined) {
      //   this.form.specialityId = "";
      // }
      this.getSpeciality();
    },

    async getSpeciality() {
      if (this.form.deptId) {
        await getSpecialityByDeptId({ deptId: this.form.deptId }).then(
          (res) => {
            if (res.code == "200") {
              this.profession = res.data;
            } else {
              this.profession = [];
            }
          }
        );
      } else {
        this.profession = [];
      }
    },
    getGradeInfor() {
      getGradeSelect().then((response) => {
        // console.log(response, "Student");
        this.gradeDate = response.data;
      });
    },
    // 收入身份证获取年月日
    codeBulr() {
      // this.form.birthday = this.getBirthday(this.form.cardCode)
      this.$set(this.form, "birthday", this.getBirthday(this.form.cardCode));
    },
    getTree() {
      getProvinceTree().then((response) => {
        this.options = response.data;
      });
    },
    getClassList() {
      getClassSelect({
        gradeId: this.form.gradeId,
        specialtyId: this.form.specialityId,
      }).then((res) => {
        this.classList = res.data;
        console.log(res, "班级");
      });
    },
    // 获取班级拉下树
    changeGrade(value) {
      if (value != undefined) {
        if (this.form.specialityId && this.form.gradeId) {
          this.getClassList();
          this.classList = [];
          this.form.classId = "";
        } else {
          if (!value) {
            this.classList = [];
            this.form.classId = "";
          }
        }
        let obj = {};
        obj = this.gradeDate.find(function (i) {
          // console.log(i,'i')
          return i.VALUE === value;
        });
        this.form.joinDate = obj.ENROLTIME;
        this.form.guaduateDate = obj.GRADUATE;
      } else {
        this.getClassList();
      }
    },
  },
};
</script>

<style scoped>
.formItem {
  margin-top: 12px;
  margin-bottom: 12px;
  width: 100%;
  padding: 0 10px;
}
.asterisk {
  color: red;
  margin-right: 3px;
}
.elSelect{
  width: 100%;
}
</style>